#include "file2hashname.h"
#include "base64enckai.h"
#include "path_char.h"
#include <string>
#include <time.h>
#include <sys/stat.h>
#include <stdlib.h>






extern "C" unsigned long long __stdcall hashkansu(char *p, unsigned int sz) {
static unsigned long long x, tmp;
static unsigned int LSZ = sizeof(long long);
unsigned int k=0;
x = 0;
while( k + LSZ <= sz ) { x ^= *(unsigned long long*) &p[k]; k += LSZ; }
if(k>=sz) return x;
tmp = 0; memcpy(&tmp, &p[k], sz-k); x ^= tmp;
return x; }



extern "C" char* __stdcall file2hashname(const std::string &fm) {
static time_t base_t; 
static struct _stat x;
static unsigned long long hash;
static std::string ch( sizeof(int) + sizeof(hash) + sizeof(x.st_size) , '\0');
static char flg=0; 
static std::wstring fn;
fn =  char2wchar(fm);
if(!flg) { flg=1; memset( &base_t, 0 ,sizeof(base_t) ); }
if( _wstat( &fn[0], &x ) != 0 ) return "";
int diff = (int) difftime( x.st_mtime, base_t );
unsigned int n=0;
memcpy( &ch[n], &x.st_size, sizeof(x.st_size) ); n+=sizeof(x.st_size);
memcpy( &ch[n], &diff, sizeof(diff) ); n+=sizeof(diff);
hash = hashkansu((char*)&fn[0], fn.size()*sizeof(fn[0]) );
memcpy( &ch[n], &hash, sizeof(hash) );
static std::string buf;
buf = base64enckai(ch);
return &buf[0]; }